//
//  RadarScene.m
//  LegoClient
//
//  Created by Balazs Faludi on 16.01.10.
//  Copyright 2010 Universität Basel. All rights reserved.
//

#import "RadarScene.h"
#import "XIParticle.h"
#import "XIVector.h"

@implementation RadarScene

@synthesize scannerWidth, scannerOn;

void initParticle(XIParticle *p) {
	XIParticleEmiterDefaultInitFunction(p);
	p->color = xiPaint(0.0f, 1.0f, 0.2f, 1.0f);
	p->child->pos = xivmult(xivneg(p->vec), 30.0f);
	p->child->color = p->color;
}

void stepParticle(XIParticle *p) {
	//XIFloat multiplier = [[XIGame sharedGame] multiplier];
	//p->pos = xivadd(p->pos, xivmult(p->vec, multiplier));
	p->color.a -= 0.0075f;
	p->child->color = p->color;
	if(p->color.a <= 0.0f) {
	//if(p->age > p->life) {
		XIParticleKill(p);
	}
}

- (id)init {
    if ((self = [super init])) {
		
		glGenTextures(10, textures);
		glBindTexture(GL_TEXTURE_2D, textures[0]);
		[XITextureLoader loadTexture:@"radarwhite"];
		
		//glBindTexture(GL_TEXTURE_2D, textures[1]);
		//[XITextureLoader loadTexture:@"spSmoke1"];
		
		
		emiter = XIParticleEmiterCreate(350);
		//emiter->texture = textures[0];
		emiter->pos = xiv(240.0, 240.0);
		emiter->particles->maxNodes = 350;
		//emiter->blendMode = XIParticleEmiterBlendAdd;
		emiter->renderMode = XIParticleEmiterRenderSeparate;
		emiter->type = XIParticleEmiterTypeLine;
		//emiter->initFunction = initParticle;
		emiter->stepFunction = stepParticle;
		emiter->emitAmount = 0;
		emiter->emitInterval = 10.0f;
		XIParticleEmiterStart(emiter);
		
		ta = 0; td = 30;
		ts = 1;
		
		//_pFont = [[XIFont alloc] initWithFontDef:@"FontDenmark24.fnt" withTexture:@"FontDenmark24"];
    }
    return self;
}

- (XIVector)getVectorForAngle:(int)angle distance:(int)distance {
	int a = 90 - angle;
	float pixelDist = distance / kMaxDistf * 240.0f;
	if(angle < 0) a = -a;
	if(angle == 0) return xiv(0.0, -pixelDist);
	float st = tanf(a / 180.0f * M_PI); // Steigung
	XIVec v = xiv(1.0f, st);
	v = xivsetlength(v, pixelDist);
	v.y = -v.y;
	v.x = -v.x;
	if(angle < 0) v.x = -v.x;
	return v;
}

- (void)update
{	
	[super update];
	
	XIParticleEmiterStep(emiter);
}

- (void)draw
{
	
	glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT);
	
	[super draw];
	
	glEnable(GL_TEXTURE_2D);
	glBindTexture(GL_TEXTURE_2D, textures[0]);

	GLfloat	 coordinates[] = {0, 1, 1, 1, 0, 0, 1, 0};
	GLfloat	vertices[] = {0.0, 320.0, 
		480.0, 320.0, 
		0.0, 0.0, 
		480.0,  0.0 };
	glVertexPointer(2, GL_FLOAT, 0, vertices);
	glTexCoordPointer(2, GL_FLOAT, 0, coordinates);
	glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
	
	BOOL rendered = XIParticleEmiterRender(emiter);
	
	/*[XIBrush drawCrossAtPoint:xivToCGPoint(emiter->pos) withColor:[XIColor blueColor]];
	CGPoint pos = xivToCGPoint(emiter->pos);
	float size = 20.0;
	CGRect rect = CGRectMake(pos.x - size * 0.5, pos.y - size * 0.5, size, size);
	[XIBrush drawRect:rect withColor:[XIColor blueColor]];*/
	if(rendered && scannerOn) {
		glPushMatrix();
		glTranslatef(emiter->pos.x, emiter->pos.y, 0.0f);
		glBlendFunc(GL_SRC_ALPHA, GL_ONE);
		glDisable(GL_TEXTURE_2D);
		glEnable(GL_BLEND);
		XIVector leader = [self getVectorForAngle:lastMeasurement.angle distance:(int)kMaxDistf];
		int slaveAngle = toRight ? max(-90.0, lastMeasurement.angle - scannerWidth) : min(90.0, lastMeasurement.angle + scannerWidth);
		XIVector slave =  [self getVectorForAngle:slaveAngle distance:(int)kMaxDistf];
		GLfloat coords[] = {0.0, 0.0, leader.x, leader.y, slave.x, slave.y};
		XIPaint color = xiPaint(0.0f, 1.0f, 0.0f, 1.0f);
		GLfloat colors[] = {color.r, color.g, color.b, color.a * 0.5, color.r, color.g, color.b, color.a, color.r, color.g, color.b, 0.0f};
		glColorPointer(4, GL_FLOAT, 0, colors);
		glEnableClientState(GL_COLOR_ARRAY);
		glVertexPointer(2, GL_FLOAT, 0, coords);
		glDrawArrays(GL_TRIANGLES, 0, 3);
		glPopMatrix();
		glEnable(GL_TEXTURE_2D);
		glDisableClientState(GL_COLOR_ARRAY);
		glEnableClientState(GL_TEXTURE_COORD_ARRAY);
	}
	
}

- (void)addAngle:(int)angle distance:(int)distance {
	distance = min(distance, kMaxDistf);
	if(lastMeasurement.angle == 0 && lastMeasurement.distance == 0) {
		lastMeasurement.angle = angle;
		lastMeasurement.distance = distance;
	}
	XIParticle *p = XIParticleCreate();
	p->pos = [self getVectorForAngle:angle distance:distance];
	p->child = XIParticleCreate();
	p->child->pos = xivsub([self getVectorForAngle:lastMeasurement.angle distance:lastMeasurement.distance], p->pos);
	p->color = xiPaint(0.0f, 1.0f, 0.0f, 1.0f);
	p->child->color = p->color;
	
	/*float colorConst = 150.0;
	p->color.r = (colorConst - distance) / colorConst;
	p->child->color.r = (colorConst - lastMeasurement.distance) / colorConst;
	p->color.g -= p->color.r;
	p->child->color.g -= p->child->color.r;*/
	
	//if(distance < 150 && lastMeasurement.distance < 150) {
		XIParticleEmiterAddParticle(emiter, p);
	//}
	
	toRight = angle > lastMeasurement.angle;
	
	lastMeasurement.angle = angle;
	lastMeasurement.distance = distance;
}

- (void)dealloc {
    [super dealloc];
}


@end
